import pygame
import sys

pygame.init()

size=width,height = 600,600
bg=(255,255,255)
screen = pygame.display.set_mode(size,pygame.RESIZABLE)
pygame.display.set_caption('乌龟')

turtle_speed=[-4,2]

ratio = 1.0
o_turtle = pygame.image.load('乌龟.jpg')
turtle=o_turtle

oturtle_position=turtle.get_rect()
turtle_position=oturtle_position
l_head =  pygame.transform.flip(turtle,True,False)
r_head = turtle

fullscreen =False
list1=[]

def bl(o_size,size):
    for i in range(0,2):
        turtle_position[i]=round(turtle_position[i]*size[i]/o_size[i])

while True :
    o_size=size
    o_turtle_position=turtle_position
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
            
        if event.type == pygame.VIDEORESIZE:
            size = event.size
            width,height = size
            if not fullscreen :
                screen = pygame.display.set_mode(size,pygame.RESIZABLE)
            
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                turtle_speed=[0,-1]
            if event.key == pygame.K_DOWN:
                turtle_speed=[0,1]
            if event.key == pygame.K_LEFT:
                turtle=l_head
                turtle_speed=[-1,0]
            if event.key == pygame.K_RIGHT:
                turtle=r_head
                turtle_speed=[1,0]

            if event.key == pygame.K_EQUALS or event.key ==pygame.K_MINUS \
               or event.key ==pygame.K_SPACE:
                if event.key == pygame.K_EQUALS and ratio <=2:
                    ratio+=0.1
                if event.key == pygame.K_MINUS and ratio >=0.5:
                    ratio-=0.1
                if event.key ==pygame.K_SPACE:
                    ratio=1.0
                    
                turtle=pygame.transform.smoothscale(o_turtle,(int(oturtle_position.width*ratio),\
                                             int(oturtle_position.height*ratio)))

                l_head =  pygame.transform.flip(turtle,True,False)
                r_head = turtle
                
            if event.key == pygame.K_F11:
                fullscreen = not fullscreen
                if fullscreen :
                    size=width,height =1920, 1080
                    screen = pygame.display.set_mode((1920, 1080)\
                                                     ,pygame.FULLSCREEN|pygame.HWSURFACE)
                    
                else:
                    size=width,height = 600,600
                    screen = pygame.display.set_mode(size)        

    turtle_position = turtle_position.move(turtle_speed)

    if turtle_position.left < 0 or turtle_position.right > width:
        turtle = pygame.transform.flip(turtle,True,False)
        turtle_speed[0] = -turtle_speed[0]
    if turtle_position.top < 0 or turtle_position.bottom > height:
        turtle_speed[1] = -turtle_speed[1]
        
    if len(list1)<=3:
        list1.append(o_turtle_position)
        if len(list1)==3:
            if list1[0][0]==list1[2][0]:
                turtle_position[0]-=2
            elif list1[0][1]==list1[2][1]:
                turtle_position[1]-=2
    else:
        list1=[]
        
    bl(o_size,size)  
    screen.fill(bg)
    screen.blit(turtle,turtle_position)
    a=turtle_position
    pygame.display.flip()
    pygame.time.delay(10)

    
